package m.system.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;

public class DateUtil {
	public final static String YYYY_MM_DD = "yyyy-MM-dd";
	public final static String YYYY_MM = "yyyy-MM";
	public final static String HH_MM_SS = "HH:mm:ss";
	public final static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

	/**
	 * 是否到期,小于当前时间返回true , 大于等于当前时间返回false
	 *  date
	 *  pattern
	 * 
	 */
	public static Boolean isExpire(Date date,String pattern){
		return format(new Date(), pattern).compareTo(format(date, pattern)) > 0;
	}
	/**
	 * 获取 当前日期yyyy-MM-dd格式的字符串
	 * 
	 */
	public static String formatNow(){
		return format(new Date());
	}
	/**
	 * 日期格式化－将Date类型的日期格式化为String型 默认yyyy-MM-dd
	 *  date
	 * 
	 */
	public static String format(Date date){
		return format(date,YYYY_MM_DD);
	}
	/**
	 * 日期格式化－将Date类型的日期格式化为String型
	 *  date 待格式化的日期
	 *  pattern 时间样式
	 *  一个被格式化了的String日期
	 */
	public static String format(Date date, String pattern) {
		if (date == null)
			return "";
		else
			return getFormatter(pattern).format(date);
	}

	/**
	 * 把字符串日期默认转换为yyyy-mm-dd格式的Data对象
	 * 
	 *  strDate
	 * 
	 */
	public static Date format(String strDate) {
		return format(strDate, YYYY_MM_DD);
	}
	/**
	 * 把字符串日期默认转换为pattern格式的Data对象
	 *  strDate
	 *  pattern
	 * 
	 */
	public static Date format(String strDate,String pattern){
		Date d;
		if (Objects.equals(strDate, ""))
			return null;
		else
			try {
				d = getFormatter(pattern).parse(strDate);
			} catch (ParseException e) {
				return null;
			}
		return d;
	}
	/**
	 * 获取日期的第一天
	 *  date
	 * 
	 */
	public static Date getFirstDay(Date date){
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DATE,1);
		return calendar.getTime();
	}
	/**
	 * 获取当天最早时刻
	 *  date
	 * 
	 */
	public static Date getStartDay(Date date){
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY,0);
		calendar.set(Calendar.MINUTE,0);
		calendar.set(Calendar.SECOND,0);
		calendar.set(Calendar.MILLISECOND,0);
		return calendar.getTime();
	}
	/**
	 * 获取当月最早时刻
	 *  date
	 * 
	 */
	public static Date getStartMonth(Date date){
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DATE,1);
		calendar.set(Calendar.HOUR_OF_DAY,0);
		calendar.set(Calendar.MINUTE,0);
		calendar.set(Calendar.SECOND,0);
		calendar.set(Calendar.MILLISECOND,0);
		return calendar.getTime();
	}
	/**
	 * 获取当天最晚时刻
	 *  date
	 * 
	 */
	public static Date getEndDay(Date date){
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY,23);
		calendar.set(Calendar.MINUTE,59);
		calendar.set(Calendar.SECOND,59);
		calendar.set(Calendar.MILLISECOND,0);
		return calendar.getTime();
	}
	/**
	 * 获取当月最晚时刻
	 *  date
	 * 
	 */
	public static Date getEndMonth(Date date){
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DATE,1);
		calendar.add(Calendar.MONTH,1);
		calendar.add(Calendar.DATE,-1);
		calendar.set(Calendar.HOUR_OF_DAY,23);
		calendar.set(Calendar.MINUTE,59);
		calendar.set(Calendar.SECOND,59);
		calendar.set(Calendar.MILLISECOND,0);
		return calendar.getTime();
	}
	/**
	 * 获取日期的最后一天
	 *  date
	 * 
	 */
	public static Date getLastDay(Date date){
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DATE,1);
		calendar.add(Calendar.MONTH,1);
		calendar.add(Calendar.DATE,-1);
		return calendar.getTime();
	}
	/**
	 * 获取当前日期的月份范围
	 * 
	 */
	public static Date[] getDateRange(Date date){
		return new Date[]{getFirstDay(date),getLastDay(date)};
	}
	public static Date[] getYearRange(Date date){
		String year=format(date, "yyyy");
		return new Date[]{
				format(year+"-01-01 00:00:00",YYYY_MM_DD_HH_MM_SS),
				format(year+"-12-31 23:59:59",YYYY_MM_DD_HH_MM_SS)
		};
	}
	/**
	 * 获取上个月
	 *  date
	 * 
	 */
	public static Date getLastMonth(Date date){
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH,-1);
		return calendar.getTime();
	}
	/**
	 * 获取下个月
	 *  date
	 * 
	 */
	public static Date getNextMonth(Date date){
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH,1);
		return calendar.getTime();
	}
	/**
	 * 获取两个时间之间的月差距
	 *  date1
	 *  date2
	 * 
	 */
	public static int getMonthGap(Date date1,Date date2){
		Calendar c1=Calendar.getInstance();
		c1.setTime(date1);
		Calendar c2=Calendar.getInstance();
		c2.setTime(date2);
		return (c1.get(Calendar.YEAR)*12+c1.get(Calendar.MONTH))-(c2.get(Calendar.YEAR)*12+c2.get(Calendar.MONTH));
	}
	/**
	 * 获取两个时间之间的日差距
	 *  date1
	 *  date2
	 * 
	 */
	public static int getDayGap(Date date1,Date date2) {
		return new Long(Math.abs(getStartDay(date1).getTime()-getStartDay(date2).getTime())/1000/60/60/24).intValue();
	}
	/**
	 * 获取 添加的月份后
	 *  date
	 *  i 
	 * 
	 */
	public static Date getAddMonth(Date date,int i){
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH,i);
		return calendar.getTime();
	}
	/**
	 * 获取 添加的天数后
	 *  date
	 *  i 
	 * 
	 */
	public static Date getAddDate(Date date,int i){
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DATE,i);
		return calendar.getTime();
	}
	/**
	 * 是否包含在区间内
	 *  date
	 *  start_date
	 *  end_date
	 *  date_format
	 * 
	 */
	public static boolean isContain(Date date,Date start_date,Date end_date,String date_format){
		return format(start_date, date_format).compareTo(format(date, date_format)) < 1
			&& format(end_date, date_format).compareTo(format(date, date_format)) > -1;
	}
	/**
	 * 判断当前时间是否包含在区间内
	 *  start_date
	 *  end_date
	 *  date_format
	 * 
	 */
	public static boolean isContain(Date start_date,Date end_date,String date_format){
		return isContain(new Date(),start_date, end_date, date_format);
	}
	/**
	 * 获取时间的具体值
	 *  date
	 *  ci Calendar类里的常量
	 * 
	 */
	public static int get(Date date,int ci){
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(ci);
	}
	/**
	 * 添加时间的具体值
	 *  date
	 *  ci Calendar类里的常量
	 *  num 增量
	 * 
	 */
	public static Date add(Date date,int ci,int num){
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(ci, num);
		return calendar.getTime();
	}
	/**
	 * 设置时间的具体值
	 *  date
	 *  ci Calendar类里的常量
	 *  num 设置量
	 * 
	 */
	public static Date set(Date date,int ci,int num){
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(ci, num);
		return calendar.getTime();
	}
	
	public static int getWeekDay(Date date) {
		Calendar now = Calendar.getInstance();
		now.setTime(date);
		//一周第一天是否为星期天
		boolean isFirstSunday = (now.getFirstDayOfWeek() == Calendar.SUNDAY);
		//获取周几
		int weekDay = now.get(Calendar.DAY_OF_WEEK);
		//若一周第一天为星期天，则-1
		if(isFirstSunday){
			weekDay = weekDay - 1;
		}else if(weekDay==7){
			weekDay=0;
		}
		return weekDay;
	}
	private static SimpleDateFormat getFormatter(String parttern) {
		return new SimpleDateFormat(parttern);
	}

}
